package com.game.springbootstart.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.game.springbootstart.config.QueryPageParam;
import com.game.springbootstart.config.Result;
import com.game.springbootstart.entity.Category;
import com.game.springbootstart.entity.Games;
import com.game.springbootstart.entity.GamesDetails;
import com.game.springbootstart.entity.GamesUrl;
import com.game.springbootstart.mapper.CategoryMapper;
import com.game.springbootstart.service.IGamesDetailsService;
import com.game.springbootstart.service.IGamesService;
import com.game.springbootstart.service.IGamesUrlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/games-details")
public class GameDetailController {
    @Autowired
    private IGamesDetailsService gamesDetailsService;
    @Autowired
    private IGamesService gamesService;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private IGamesUrlService gamesUrlService;
    @GetMapping("/list")
    public Result list() {
        return Result.success(gamesDetailsService.list());
    }

    @PostMapping("/saveOrMod")
    public Result saveOrMod(@RequestBody GamesDetails gamesDetails) {
        return Result.success(gamesDetailsService.saveOrUpdate(gamesDetails));
    }
    @GetMapping("/delete")
    public Result delete(Integer id) {
        return Result.success(gamesDetailsService.removeById(id));
    }
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        Page<GamesDetails> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<GamesDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(GamesDetails::getName,name)
                .eq(GamesDetails::getStatus, 1);;
        IPage<GamesDetails> result = gamesDetailsService.page(page, queryWrapper);
        return Result.success(result.getRecords(), result.getTotal());
    }
    @PostMapping("/approve")
    public Result approve(@RequestParam Integer id) {
        GamesDetails games = gamesDetailsService.getById(id);
        String category = games.getCategory();
        Long CategoryId = null; // 初始化为 null
        Games game;
        if (category != null && !category.isEmpty()) {
            CategoryId = categoryMapper.selectIdByName(category);
        }
        if (CategoryId != null) {
            // CategoryId 有效时的逻辑
            game = Games.builder()
                    .name(games.getName())
                    .categoryId(CategoryId)
                    .description(games.getDescription())
                    .developer(games.getDeveloper())
                    .imageUrl(games.getImageUrl())
                    .platform(games.getPlatform())
                    .releaseDate(games.getReleaseDate())
                    .status("上线")
                    .build();
        } else {
            Category categoryObj = Category.builder()
                    .name(category)
                    .status(1)
                    .build();
            categoryMapper.save(categoryObj);
            Long categoryId = categoryMapper.selectIdByName(category);
            game = Games.builder()
                    .name(games.getName())
                    .categoryId(categoryId)
                    .description(games.getDescription())
                    .developer(games.getDeveloper())
                    .imageUrl(games.getImageUrl())
                    .platform(games.getPlatform())
                    .releaseDate(games.getReleaseDate())
                    .status("上线")
                    .build();
        }
        gamesService.save(game);
        Long gameId = gamesService.selectIdByName(games.getName());
        GamesUrl gamesUrl = new GamesUrl();
        gamesUrl.setGameId(gameId);
        gamesUrl.setGameUrl(games.getGameUrl());
        gamesDetailsService.setStatus(id);
        return Result.success(gamesUrlService.save(gamesUrl));
    }

    @PostMapping("/reject")
    public Result rejectGame(@RequestParam Integer id) {
        gamesDetailsService.setStatus(id);
        return Result.success(true);
    }
}
